package com.thb.pay.core.utils;

import com.alibaba.fastjson.JSON;
import com.thb.pay.core.dto.IpLocate;
import com.xiaoleilu.hutool.collection.CollectionUtil;
import com.xiaoleilu.hutool.http.HttpUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;


/**
 * @author lucky
 */
@Slf4j
public class IpInfoUtil {

    /**
     * Mob api
     */
    private static final String APPKEY = "你的appkey";

    private static final String LOCAL_HOST = "0:0:0:0:0:0:0:1";

    /**
     * 获取客户端IP地址
     *
     * @param request 请求
     * @return
     */
    public static String getIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
            if (ip.equals("127.0.0.1")) {
                //根据网卡取本机配置的IP
                InetAddress inet = null;
                try {
                    inet = InetAddress.getLocalHost();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                ip = inet.getHostAddress();
            }
        }
        // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
        if (ip != null && ip.length() > 15) {
            if (ip.indexOf(",") > 0) {
                ip = ip.substring(0, ip.indexOf(","));
            }
        }
        if (LOCAL_HOST.equals(ip)) {
            ip = "127.0.0.1";
        }
        return ip;
    }

    /**
     * 判断IP是否允许(强校验)
     *
     * @param ip       客户端IP
     * @param whiteIps 白名单集合
     * @param blackIps 黑名单集合
     * @return
     */
    public static Boolean ipAllow(String ip, List<String> whiteIps, List<String> blackIps) {
        // 没有IP则返回false
        if (StringUtils.isBlank(ip)) {
            return false;
        }
        // 白名单为空,返回false
        if (CollectionUtil.isEmpty(whiteIps)) {
            return false;
        }
        // 如果不在白名单,返回false
        if (!whiteIps.contains(ip)) {
            return false;
        }
        if (CollectionUtil.isEmpty(blackIps)) {
            return true;
        }
        // 如果黑名单不为空,则判断是否在黑名单中
        return blackIps.contains(ip);
    }
    /**
     * 获取IP返回地理天气信息
     *
     * @param ip ip地址
     * @return
     */
    public String getIpWeatherInfo(String ip) {

        String GET_IP_WEATHER = "http://apicloud.mob.com/v1/weather/ip?key=" + APPKEY + "&ip=";
        if (StrUtil.isNotBlank(ip)) {
            String url = GET_IP_WEATHER + ip;
            String result = HttpUtil.get(url);
            return result;
        }
        return null;
    }

    /**
     * 获取IP返回地理信息
     *
     * @param ip ip地址
     * @return
     */
    public String getIpCity(String ip) {

        String GET_IP_LOCATE = "http://apicloud.mob.com/ip/query?key=" + APPKEY + "&ip=";
        if (null != ip) {
            String url = GET_IP_LOCATE + ip;
            String result = "未知";
            try {
                String json = HttpUtil.get(url, 3000);
                IpLocate locate = JSON.parseObject(json, IpLocate.class);
                if (("200").equals(locate.getRetCode())) {
                    if (StrUtil.isNotBlank(locate.getResult().getProvince())) {
                        result = locate.getResult().getProvince() + " " + locate.getResult().getCity();
                    } else {
                        result = locate.getResult().getCountry();
                    }
                }
            } catch (Exception e) {
                log.info("获取IP信息失败");
            }
            return result;
        }
        return null;
    }
}